package com.bpms.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Decoder;

import com.bpms.common.CommonUtil;
import com.bpms.common.Db;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.SV;

public class ShowAccount extends HttpServlet {

	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);

	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/x-json");
		response.setCharacterEncoding("utf-8");

		PrintWriter out = response.getWriter();
		String type = request.getParameter("type");
		List<String> list = new ArrayList<String>();
		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
		VO vo = new VO();
		JdbcWorker j = null;
		System.out.println("---------------"+type+"--------------------");
		if ("1".equals(type)) {//根据客户签约卡查询帐号并显示
			vo.setAttr("CARDID", request.getParameter("cardId"));
			vo.setAttr("ACCOUNTTYPE", "1");
			vo.setAttr("PASSWORD", "");
			VO result = SV.g("khqy.Getavailableaccounts", vo);
			VOList volList = (VOList) result.getObj("volist");
			Vector<VO> vector = volList.listData;//迭代成一个vector形式。
			
			for (VO gvo0 : vector) {
				list.add(gvo0.getAttr("ACCOUNT"));
				list1.add(gvo0.getAttr("FULLNAME"));
				list2.add(gvo0.getAttr("bank"));
			}
				out.print(list.toString()+"@"+list1.toString()+"@"+list2.toString());
			out.close();
		}else if ("2".equals(type)) {//回单打印时显示账号
			System.out.println("jin.........");
			vo.setAttr("CARDID", request.getParameter("cardId"));
			vo.setAttr("ACCOUNTTYPE", "1");
			vo.setAttr("PASSWORD", "");
			VO result = SV.g("khqy.Getavailableaccounts", vo);
			VOList volList = (VOList) result.getObj("volist");
			Vector<VO> vector = volList.listData;
			ArrayList<String> accountList = new ArrayList<String>();
			int i = 1;
			for (VO gvo : vector) {
				accountList.add("{\"no\":\"" + i
						+ "\",\"accountno\":\"" + gvo.getAttr("ACCOUNT")
						+ "\",\"name\":\"" + gvo.getAttr("NAME") + "\"}");
						i++;
			}

			out.print(accountList.toString());
			out.close();
			
		}else if ("3".equals(type)) {// 根据身份证查询帐号并显示
			vo.setAttr("CARDID", request.getParameter("cardId"));
			vo.setAttr("ACCOUNTTYPE", "3");
			vo.setAttr("PASSWORD", "");
			VO result = SV.g("khqy.Getavailableaccounts", vo);
			VOList volList = (VOList) result.getObj("volist");
			Vector<VO> vector = volList.listData;
			for (VO gvo : vector) {
				list.add(gvo.getAttr("ACCOUNT"));
			}
			out.print(list.toString());
			out.close();
		} else if ("saveCheck".equals(type)) {// 将扫描的图片保存到服务器
			BASE64Decoder decoder = new sun.misc.BASE64Decoder();
			String image1 = request.getParameter("image1");
			String image2 = request.getParameter("image2");

			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat();
			String path = "";
			String path2 = "";
			String fileName = "正面图片" + date.getTime();// 正面图片
			String fileName2 = "背面图片" + date.getTime();// 背面图片
			byte[] bt1 = new byte[20000];
			byte[] bt2 = new byte[20000];
			if (fileName != null) {
				bt1 = decoder.decodeBuffer(image1);
				bt2 = decoder.decodeBuffer(image2);
				for (int i = 0; i < bt1.length; i++) {
					if (bt1[i] < 0) { // 调整异常数据
						bt1[i] += 256;
					}
				}

				for (int i = 0; i < bt2.length; i++) {
					if (bt2[i] < 0) { // 调整异常数据
						bt2[i] += 256;
					}
				}

			}

			sdf.applyPattern("yyyyMMdd");
			String dirname = sdf.format(date);
			File dir = new File("C:\\bills\\" + dirname);
			if (!dir.exists()) {
				dir.mkdirs();
			}// 中文图片显示
			if (!image1.equals("") && image1 != null) {
				path = dir.getAbsolutePath() + "\\"
						+ fileName.replaceAll("%", "-");
			}
			if (!image2.equals("") && image2 != null) {
				path2 = dir.getAbsolutePath() + "\\"
						+ fileName2.replaceAll("%", "-");
			}

			OutputStream fos = new FileOutputStream(new File(path + ".jpg"),
					false);
			OutputStream fos2 = new FileOutputStream(new File(path2 + ".jpg"),
					false);
			fos.write(bt1);
			fos.flush();
			fos.close();
			fos2.write(bt2);
			fos2.flush();
			fos2.close();

			// 保存数据
			try {
				j = new JdbcWorker();
				j.startTransaction();
				Long instanceid = j.getSeqValue("cmdjb_bizid");
				System.out.println("instanceid:.............."+instanceid);
				String bizid = new SimpleDateFormat("yyMMddHHmmss")
						.format(date) + String.format("%08d", instanceid);
				vo.voType = "CM_DJB";

				vo.setAttr("INSTANCEID", Long.toString(instanceid));// ID号
				vo.setAttr("BIZID", bizid); // 流水号
				vo.setAttr("CARDID", request.getParameter("currentaccount"));// 当前账号
				vo.setAttr("DEVICENO", request.getParameter("deviceno"));// 设备号
				vo.setAttr("NETNUM", request.getParameter("netnum"));// 网络号
				vo.setAttr("XNGYH", request.getParameter("dianZiGuiYuan"));//虚拟柜员号
				vo.setAttr("TRANTYPE", request.getParameter("trans_type"));// 票据类型
				vo.setAttr("RESULTS", request.getParameter("result"));// 操作结果
																		// “0”表示异常交易
																		// “1”表示正常交易
				vo.setAttr("PAYEENAME", request.getParameter("payer"));//付款人全称(出票人)
				vo.setAttr("PAYEEACCNO",request.getParameter("p_account"));//付款人账号
				vo.setAttr("AMOUNT",request.getParameter("money"));// 金额
				vo.setAttr("PAYEEBANK", request.getParameter("opening_bank"));//付款人开户行
				
				sdf.applyPattern("yyyy-MM-dd");
				String ymd = sdf.format(date);
				sdf.applyPattern("HH:mm:ss");
				String hms = sdf.format(date);
				vo.setAttr("CREATEDATE", ymd);// 日期
				vo.setAttr("CREATETIME", hms);// 时间

				vo.setAttr("IMAGE1", path + ".jpg");
				vo.setAttr("IMAGE2", path2 + ".jpg");
				j.insert(vo);
				j.commit();
				out.print(instanceid);
				out.close();
			} catch (Exception e) {
				CommonUtil.debug("insertdata exception", e);
				vo.setAttr("result", "9999");
				vo.setAttr("msg", "插入错误");
				try {
					j.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

		} else if ("login".equals(type)) {// 查询清机人帐号和密码是否正确
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");

			List<String> info = new ArrayList<String>();
			if (username.equals("") || username == null || "".equals(pwd)
					|| pwd == null) {
				info.add("9999");
				info.add("参数为空");
				out.print(info.toString());
				out.close();
				return;
			}
			try {
				Db db = new Db();
				StringBuffer userSql = new StringBuffer();
				userSql.append("select * from CLEANERINFO where USERNAME='");
				userSql.append(username);
				userSql.append("'");
				VO uservo = db.readInCombination(new VO(), userSql.toString());
				if (uservo == null) {
					info.add("9999");
					info.add("没有该用户，请从新输入");
					out.print(info.toString());
					out.close();
					return;
				}

				if (!pwd.equals(uservo.getAttr("PWD"))) {
					info.add("9999");
					info.add("用户密码错误");
					out.print(info.toString());
					out.close();
					return;
				} else {
					System.out.println("4");
					info.add("0000");
					info.add("用户密码正确");
					out.print(info.toString());
					out.close();
					return;
				}

			} catch (Exception e) {
				CommonUtil.debug("cleaner.login exception", e);
				// e.printStackTrace();
				info.add("9999");
				info.add("系统异常");
				out.print(info.toString());
				out.close();
				return;
			}

		} else if ("cleanInfo".equals(type)) {
			try {
				j = new JdbcWorker();
				j.startTransaction();
				try {
					String sql = "select HZDYS,HDDYS,CARDID,CREATEDATE，CREATETIME,DRAWEENAME,TRANTYPE,RESULTS from CM_DJB  WHERE isCleanMachine='0'";
					VOList volist = j.readAll(new VO(), sql);

					Vector<VO> vector = volist.listData;
					int hddaysum = 0;//统计回单打印数
					int hzdyssum = 0;//统计回执打印数
					int i = 0;
					ArrayList<String> arr = new ArrayList<String>();

					int m = 0;//记录支票总数
					int n = 0;//记录贷记凭证总数
					int k = 0;//记录进账单总数
					int js = 0;//结算任务申请书总数
					int tc = 0;//同城支付凭证
					for (VO vo2 : vector) {
						hddaysum += Integer.parseInt(vo2.getAttr("HDDYS"));//统计回单打印数
						hzdyssum += Integer.parseInt(vo2.getAttr("HZDYS"));//统计回执打印数
						String cardId = vo2.getAttr("CARDID");
						String createDate = vo2.getAttr("CREATEDATE");// 创建日期
						String createTime = vo2.getAttr("CREATETIME");// 创建时间
//						String draweename = vo2.getAttr("DRAWEENAME");// 收款人全称
						String tranType = vo2.getAttr("TRANTYPE");// 票据类型
						
						if ("01".equals(tranType)) {
							tranType = "支票";
							m++;
						}  else if("02".equals(tranType)){
							tranType = "贷记凭证";
							n++;
						}  else if("03".equals(tranType)) {
							tranType = "结算任务申请书";
							js++;
						} else if("04".equals(tranType)) {
							tranType = "同城支付凭证";
							tc++;
						} 

						String result = vo2.getAttr("RESULTS");// 操作结果
						if("1".equals(result)){
							k++;
							result = "正常交易";
						}else if("0".equals(result)){
							result = "未完成交易";
						}
						String time = createDate + " " + createTime;
						i++;
						arr.add("{\"dataField1\":\"" + i
								+ "\",\"dataField2\":\"" + cardId
								+ "\",\"dataField3\":\"" + "用户名"
								+ "\",\"dataField4\":\"" + tranType
								+ "\",\"dataField5\":\"" + result
								+ "\",\"dataField7\":\"" + time + "\"}");
					}
					
					SimpleDateFormat format1 = new SimpleDateFormat(
							"yyyyMMddhhmmss");
					Date date = new Date();
					String zzpch=format1.format(date)
							+ (int)Math.floor((Math.random() * 1000));

					// 更新数据库isCleanMachine=1
					String updateStr = "update CM_DJB  set isCleanMachine="+zzpch+" where isCleanMachine='0'";
					j.execute(updateStr);

					String dd = date.toLocaleString();
					int pagepage=i/24;
					if(i%24!=0){
						pagepage++;
					}
					list.add(dd.substring(0, 9));// 合计日期
					list.add(dd.substring(10, 18));// 时间
					list.add(i + "");// 受理票据数
					list.add(hzdyssum + "");// 回执打印数
					list.add(hddaysum + "");// 回单打印数
					list.add("12");// 历史明细打印数
					list.add(request.getParameter("dianZiGuiYuan"));// 虚拟柜员号
					list.add(pagepage+"");// 收票清单页数
					list.add(zzpch);// 扎帐批次号
//					list.add(request.getParameter("netNum"));// 网络号
					list.add(m+"");//支票总数
					list.add(k+"");//进账单总数
					list.add(n+"");//贷记凭证总数（1,2联）
					list.add(n+"");//贷记凭证总数（3,4联）
					
					list.add(tc+"");//同城支付凭证总数
					list.add(js+"");//结算任务申请书总数
					list.add(i+hddaysum+"");

					// 保存数据
					vo.voType = "CLEANLIST";// 插入表
					vo.setAttr("OPERDATE", dd.substring(0, 10));// 合计日期
					vo.setAttr("OPERTIME", dd.substring(11, 18)); // 时间
					vo.setAttr("SLPJS", i + "");// 受理票据数
					vo.setAttr("PJHZDYS", hzdyssum + "");// 回执打印数
					vo.setAttr("HDDYS", hddaysum + "");// 回单打印数
					vo.setAttr("LSMXDYS", "12");// 历史明细打印数
					vo.setAttr("XNGYH", "虚拟柜员号");// 虚拟柜员号
					vo.setAttr("SPQDYS", "收票清单页数"); // 收票清单页数
					vo.setAttr("ZZPCH",zzpch);// 扎帐批次号
					
					vo.setAttr("NETNUM", "1234");// 网络号
					vo.setAttr("CLEANNAME", "1234");// 清机人
					j.insert(vo);
					out.print(list.toString());
					out.print("{");
					out.print(arr.toString());	
					out.close();
					j.commit();
				} catch (Exception e) {
					e.printStackTrace();	
					j.rollback();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if ("receiptprint".equals(type)) {// 回执打印
			try {
				SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
				Date date = new Date();
				String dd = format.format(date);
				j = new JdbcWorker();
				String sql = "select * from CM_DJB  WHERE INSTANCEID='"
						+ request.getParameter("instanceid") + "'";

				VO resultvo = j.readInCombination(new VO(), sql);
				System.out.println("sql:......."+sql);
				String tranType = resultvo.getAttr("TRANTYPE");
				if ("01".equals(tranType)) {
					tranType = "支票";
				}  else if("02".equals(tranType)){
					tranType = "贷记凭证";

				} else if("03".equals(tranType)) {
					tranType = "结算业务申请书";
				} else if("04".equals(tranType)) {
					tranType = "同城支付凭证";
				}

				String resultstr = resultvo.getAttr("CARDID") + "@" // 账号
						+ "用户名" + "@" // 账号
						+ tranType + "@" // 业务类型
						+ 1 + "@" // 村票数
						+ resultvo.getAttr("CREATETIME") + "@" // 存入时间
						+ resultvo.getAttr("XNGYH") + "@" // 虚拟柜员号
						+ dd + "@" // 打印时间
						+ resultvo.getAttr("BIZID") + "@"; // 回执编号

				out.print(resultstr.toString());
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}else if("testNet".equals(type)){//测试网络
			out.print("good");
			out.close();
		}else if("HDDY".equals(type)){//回单补打的账号
			try {
				j=new JdbcWorker();
				String sql = "select * from cm_djb where  STATUS = '1' and RZHD = '0'";
				VOList volist = j.readAll(new VO(), sql);

				ArrayList<String> arr = new ArrayList<String>();
				Vector<VO> vector = volist.listData;
				int i=1;//序号
				for (VO vo2 : vector) {
					String araweeaccno=vo2.getAttr("DRAWEEACCNO");//收款人账号
					String payeeaccno=vo2.getAttr("PAYEEACCNO");//付款人账号
					String amount=vo2.getAttr("AMOUNT");//金额
					String dycs=vo2.getAttr("DYCS");//打印次数
					
					arr.add("{\"no\":\"" + i
							+ "\",\"araweeaccno\":\"" + araweeaccno
							+ "\",\"payeeaccno\":\"" + payeeaccno
							+ "\",\"amount\":\"" + amount
							+ "\",\"dycs\":\"" + dycs + "\"}");
					i++;
				}
				out.print(arr.toString());	
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}else if("receiptDetail".equals(type)){//账号下的交易记录
			try {
				j=new JdbcWorker();
				String isAgainPrint=request.getParameter("isAgainPrint")+"";
				String starttime=request.getParameter("starttime");
				String endtime=request.getParameter("endtime");
//				starttime=starttime.replace('年','-');
//				starttime=starttime.replace('月','-');
//				starttime=starttime.replace('日','-');
//				
//				endtime=endtime.replace('年', '-');
//				endtime=endtime.replace('月', '-');
//				endtime=endtime.replace('日', '-');
				
				StringBuilder sql = new StringBuilder("select * from HDINFO where  ACCOUNT='");
				sql.append(request.getParameter("account")).append("'");
				if("true".endsWith(isAgainPrint)){
					sql.append(" and DYCS!='0'");
				}else{
					sql.append(" and DYCS='0'");
				}
				sql.append(" and CREATEDATE between '");
				sql.append(starttime).append("' and '");
				sql.append(endtime).append("'");
				
				
				
				String pageNumber=request.getParameter("number");
				String pageSize=request.getParameter("size");
				
				String sqlstr = "select * from (select mypager.*,rownum num from ( " + sql.toString() + " ) mypager) ";
				sqlstr += "where num > ("+pageNumber+"-1) * "+pageSize+" and num <="+pageNumber+" * "+pageSize;
				
				String limit = request.getParameter("limit");
				VOList volist=new VOList();
				String cc = "";
				if("part".equals(limit)){
					volist = j.readAll(new VO(), sqlstr);
					if("1".equals(pageNumber)){
						StringBuilder count = new StringBuilder("select count(*) from HDINFO where  ACCOUNT='");
						count.append(request.getParameter("account")).append("'");
						if("true".endsWith(isAgainPrint)){
							count.append(" and DYCS!='0'");
						}else{
							count.append(" and DYCS='0'");
						}
						count.append(" and CREATEDATE between '");
						count.append(starttime).append("' and '");
						count.append(endtime).append("'");
						VO counts=j.readInCombination(new VO(),count.toString());
						cc = counts.getAttr("count(*)");
					}
					
				}else if("all".equals(limit)){
					volist = j.readAll(new VO(), sql.toString());
				}

				ArrayList<String> arr = new ArrayList<String>();

				ArrayList<String> arr2 = new ArrayList<String>();//存放要打印的信息
				Vector<VO> vector = volist.listData;
				int i=1;//序号
				for (VO vo2 : vector) {
					String id=vo2.getAttr("ID");//id号//
					String account=vo2.getAttr("ACCOUNT");//账号
					System.out.println("account....:"+account);
					String payeeaccno=vo2.getAttr("PAYEEACCNO");//出票人账号
					String payeename=vo2.getAttr("PAYEENAME");//出票人全称
					String amount=vo2.getAttr("AMOUNT");//金额
					String dycs=vo2.getAttr("DYCS");//打印次数
					String payeebank=vo2.getAttr("PAYEEBANK");//出票人开户银行
					String draweename=vo2.getAttr("DRAWEENAME");//收款人全称
					String draweeaccno=vo2.getAttr("DRAWEEACCNO");//收款人账号
					String draweebank=vo2.getAttr("DRAWEEBANK");//收款人开户行
					String billtype=vo2.getAttr("BILLTYPE");//票据种类
					String bills=vo2.getAttr("BILLS");//票据张数
					String billno=vo2.getAttr("BILLNO");//票据号码
					
					arr2.add("{\"payeename\":\"" + payeename
							
							+ "\",\"no\":\"" + i
							+ "\",\"dycs\":\"" + dycs
							+ "\",\"id\":\"" + id
							
							+ "\",\"payeeaccno\":\"" + payeeaccno
							+ "\",\"payeebank\":\"" + payeebank
							+ "\",\"draweename\":\"" + draweename
							+ "\",\"draweeaccno\":\"" + draweeaccno
							+ "\",\"draweebank\":\"" + draweebank
							+ "\",\"amount\":\"" + amount
							+ "\",\"billtype\":\"" + billtype
							+ "\",\"bills\":\"" + bills
							+ "\",\"billno\":\"" + billno + "\"}");
					i++;
					
					int DYCS=Integer.parseInt(dycs);
					DYCS++;
					
					//更新打印次数
					if("all".equals(limit)){
						StringBuilder sqlStr = new StringBuilder("update HDINFO set DYCS='");
						sqlStr.append(dycs+"' where ID='").append(id).append("'");
						j.execute(sqlStr.toString());
						
						//更新CM_DJB中的打印次数
						StringBuilder sqlToCm_djb=new StringBuilder("update CM_DJB set HDDYS='");
						sqlToCm_djb.append(DYCS+"' where CARDID='").append(account).append("'");
						j.execute(sqlToCm_djb.toString());
					}
				}
				out.print(cc+"*");
				out.print(arr2.toString());	
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}else if("updateDYCS".equals(type)){
			try {
				j=new JdbcWorker();
				String update = request.getParameter("updateArr");
				String[] updateArr = update.split(",");
				for(int i=0;i<updateArr.length;i++){
					String[] Arr = updateArr[i].split("@");
					StringBuilder sql = new StringBuilder("update HDINFO set DYCS='");
					sql.append(Arr[1]+"' where ID='").append(Arr[0]).append("'");
					j.execute(sql.toString());
					
					//更新CM_DJB中的打印次数
					StringBuilder sqlToCm_djb=new StringBuilder("update CM_DJB set HDDYS='");
					sqlToCm_djb.append(Arr[1]+"' where CARDID='").append(Arr[2]).append("'");
					j.execute(sqlToCm_djb.toString());
				}
				j.commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		} else if("queryCode".equals(type)) {
			try {
				j = new JdbcWorker();
				String code = request.getParameter("code");
				StringBuilder sql = new StringBuilder("select * from dimechart where encryption='");
				sql.append(code).append("'");
				
				
				VO vol = j.readInCombination(new VO(), sql.toString());
				String aa;
				if(vol == null) {
					aa = "nodata";
				} else {
					aa = "data";
				}
				
				out.print(aa);
				out.close();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}else if("payinput".equals(type)){ //验证非ID卡用户登录。并返回该用户的信息。
			String name=request.getParameter("payeename");
			String account=request.getParameter("payeeaccount");
			String payeebank=request.getParameter("payeebank");
			vo.setAttr("pname", name);
			vo.setAttr("paccount", account);
			vo.setAttr("payeebank", payeebank);
			VO result = SV.g("khqy.SelectPayee", vo);
		    	result.getAttr("");
			String payee= result.getAttr("account").toString()+","+result.getAttr("" +
					"").toString()+","+result.getAttr("bank");
			out.print(payee);
		}else if("saveCheckBS".equals(type)){// 将扫描的图片保存到服务器
			BASE64Decoder decoder = new sun.misc.BASE64Decoder();
			String image1 = request.getParameter("image1");
			String image2 = request.getParameter("image2");
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat();
			String path = "";
			String path2 = "";
			String fileName = "正面图片" + date.getTime();// 正面图片
			String fileName2 = "背面图片" + date.getTime();// 背面图片
			byte[] bt1 = new byte[20000];
			byte[] bt2 = new byte[20000];
			if (fileName != null) {
				bt1 = decoder.decodeBuffer(image1);
				bt2 = decoder.decodeBuffer(image2);
				for (int i = 0; i < bt1.length; i++) {
					if (bt1[i] < 0) { // 调整异常数据
						bt1[i] += 256;
					}
				}
				for (int i = 0; i < bt2.length; i++) {
					if (bt2[i] < 0) { // 调整异常数据
						bt2[i] += 256;
					}
				}

			}

			sdf.applyPattern("yyyyMMdd");
			String dirname = sdf.format(date);
			File dir = new File("C:\\bills\\" + dirname);
			if (!dir.exists()) {
				dir.mkdirs();
			}// 中文图片显示
			if (!image1.equals("") && image1 != null) {
				path = dir.getAbsolutePath() + "\\"
						+ fileName.replaceAll("%", "-");
			}
			if (!image2.equals("") && image2 != null) {
				path2 = dir.getAbsolutePath() + "\\"
						+ fileName2.replaceAll("%", "-");
			}

			OutputStream fos = new FileOutputStream(new File(path + ".jpg"),
					false);
			OutputStream fos2 = new FileOutputStream(new File(path2 + ".jpg"),
					false);
			fos.write(bt1);
			fos.flush();
			fos.close();
			fos2.write(bt2);
			fos2.flush();
			fos2.close();
			// 保存数据
			try {
				j = new JdbcWorker();
				j.startTransaction();
				Long instanceid = j.getSeqValue("cmdjb_bizid");
				System.out.println("instanceid:.............."+instanceid);
				String bizid = new SimpleDateFormat("yyMMddHHmmss")
						.format(date) + String.format("%08d", instanceid);
				vo.voType = "CM_DJB";

				vo.setAttr("INSTANCEID", Long.toString(instanceid));// ID号
				vo.setAttr("BIZID", bizid); // 流水号
			//	vo.setAttr("CARDID", request.getParameter("currentaccount"));// 当前账号
				vo.setAttr("DRAWEENAME",request.getParameter("payername") );//付款人全称
				vo.setAttr("DRAWEEACCNO",request.getParameter("payeraccount") );//付款人账号
				vo.setAttr("DRAWEEBANK",request.getParameter("payerbank") );//付款人开户行
				vo.setAttr("PAYEENAME",request.getParameter("payeename") );//收款人全称
				vo.setAttr("PAYEEACCNO",request.getParameter("payeeaccount") );//收款人账号
				vo.setAttr("PAYEEBANK",request.getParameter("PayeeBank") );//收款人开户行
				vo.setAttr("DEVICENO", request.getParameter("deviceno"));// 设备号
				vo.setAttr("NETNUM", request.getParameter("netnum"));// 网络号
				vo.setAttr("XNGYH", request.getParameter("dianZiGuiYuan"));//虚拟柜员号
				vo.setAttr("TRANTYPE", request.getParameter("trans_type"));// 票据类型
				vo.setAttr("RESULTS", request.getParameter("result"));// 操作结果
																		// “0”表示异常交易
																		// “1”表示正常交易
			//	vo.setAttr("PAYEENAME", request.getParameter("payer"));//付款人全称(出票人)
			//	vo.setAttr("PAYEEACCNO",request.getParameter("p_account"));//付款人账号
				vo.setAttr("AMOUNT",request.getParameter("money"));// 金额
				//vo.setAttr("PAYEEBANK", request.getParameter("opening_bank"));//付款人开户行
				
				sdf.applyPattern("yyyy-MM-dd");
				String ymd = sdf.format(date);
				sdf.applyPattern("HH:mm:ss");
				String hms = sdf.format(date);
				vo.setAttr("CREATEDATE", ymd);// 日期
				vo.setAttr("CREATETIME", hms);// 时间

				vo.setAttr("IMAGE1", path + ".jpg");
				vo.setAttr("IMAGE2", path2 + ".jpg");
				j.insert(vo);
				j.commit();
				out.print(instanceid);
				out.close();
			} catch (Exception e) {
				CommonUtil.debug("insertdata exception", e);
				vo.setAttr("result", "9999");
				vo.setAttr("msg", "插入错误");
				try {
					j.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

		
			
		}else if ("receiptprint".equals(type)) {// 回执打印
			try {
				SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
				Date date = new Date();
				String dd = format.format(date);
				j = new JdbcWorker();
				String sql = "select * from CM_DJB  WHERE INSTANCEID='"
						+ request.getParameter("instanceid") + "'";
				VO resultvo = j.readInCombination(new VO(), sql);
				System.out.println("sql:......."+sql);
				String tranType = resultvo.getAttr("TRANTYPE");
				if ("01".equals(tranType)) {
					tranType = "支票";
				}  else if("02".equals(tranType)){
					tranType = "贷记凭证";
				} else if("03".equals(tranType)) {
					tranType = "结算业务申请书";
				} else if("04".equals(tranType)) {
					tranType = "同城支付凭证";
				}

				String resultstr = resultvo.getAttr("PAYEEACCNO") + "@" // 收款人账号
						+ resultvo.getAttr("PAYEENAME") + "@" // 收款人全称
						+ resultvo.getAttr("PAYEEBANK") + "@" //收款人开户行
						+ resultvo.getAttr("DRAWEEACCNO") + "@" // 付款人帐号
						+ resultvo.getAttr("DRAWEENAME") + "@" // 付款人全称
						+ resultvo.getAttr("DRAWEEBANK") + "@" // 付款人开户行
						+ resultvo.getAttr("AMOUNT") + "@" // 交易金额
						+ tranType + "@" // 业务类型
						+ 1 + "@" // 村票数
						+ resultvo.getAttr("CREATETIME") + "@" // 存入时间
						+ resultvo.getAttr("XNGYH") + "@" // 虚拟柜员号
						+ dd + "@" // 打印时间
						+ resultvo.getAttr("BIZID") + "@"// 回执编号
						+ resultvo.getAttr("BILLNO") + "@"; //票据单号
				out.print(resultstr.toString());
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
         
		/*
		 * protected void service(HttpServletRequest request,
		 * HttpServletResponse response) throws ServletException, IOException {
		 * response.setContentType("application/x-json");
		 * response.setCharacterEncoding("utf-8"); PrintWriter
		 * out=response.getWriter(); String type=request.getParameter("type");
		 * System.out.println(type+"..........."); VO vo = new VO();;
		 * if("1".equals(type)){
		 * vo.setAttr("CARDID",request.getParameter("cardId"));
		 * vo.setAttr("ACCOUNTTYPE", "1"); vo.setAttr("PASSWORD", ""); VO result
		 * = SV.g("khqy.Getavailableaccounts", vo); VOList
		 * volList=(VOList)result.getObj("volist"); Vector<VO> vector =
		 * volList.listData; List<String> list=new ArrayList<String>(); for (VO
		 * gvo : vector) { list.add(gvo.getAttr("ACCOUNT")); }
		 * 
		 * out.print(list.toString()); out.close();
		 * 
		 * }else if("3".equals(type)){
		 * vo.setAttr("CARDID",request.getParameter("cardId"));
		 * vo.setAttr("ACCOUNTTYPE", "3"); vo.setAttr("PASSWORD", ""); VO result
		 * = SV.g("khqy.Getavailableaccounts", vo); VOList
		 * volList=(VOList)result.getObj("volist"); Vector<VO> vector =
		 * volList.listData; List<String> list=new ArrayList<String>(); for (VO
		 * gvo : vector) { list.add(gvo.getAttr("ACCOUNT")); }
		 * out.print(list.toString()); out.close();
		 * 
		 * }else if("saveCheck".equals(type)){ BASE64Decoder decoder = new
		 * sun.misc.BASE64Decoder(); String
		 * image1=request.getParameter("image1"); String
		 * image2=request.getParameter("image2"); InputStream inputStream =
		 * request.getInputStream();
		 * 
		 * String fileName = "图片1"; String fileName2 = "图片2"; byte[] bt1 = new
		 * byte[100000000]; byte[] bt2 = new byte[100000000];
		 * if(fileName!=null){ bt1 =decoder.decodeBuffer(image1);
		 * bt2=decoder.decodeBuffer(image2); for(int i=0;i<bt1.length;i++){
		 * if(bt1[i]<0){ //调整异常数据 bt1[i]+=256; bt2[i]+=256; } } }
		 * 
		 * File dir = new File("C:\\bills\\20120806"); if (!dir.exists()) {
		 * dir.mkdirs(); }//中文图片显示 String path = dir.getAbsolutePath() + "/" +
		 * fileName.replaceAll("%", "-"); String path2 = dir.getAbsolutePath() +
		 * "/" + fileName2.replaceAll("%", "-"); OutputStream fos= new
		 * FileOutputStream(new File(path+".jpg"),false); OutputStream fos2= new
		 * FileOutputStream(new File(path2+".jpg"),false); fos.write(bt1);
		 * 
		 * fos.flush(); fos.close(); fos2.write(bt2); fos2.flush(); fos.close();
		 * fileName=java.net.URLDecoder.decode(fileName,"utf-8");
		 * out.print(fileName+".jpg"); }else {
		 * 
		 * }
		 * 
		 * 
		 * }
		 */

	}
}
